﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tecnopez.Globals;
using BussinessLogicTecnoPez;
using BussinessLogicTecnoPez.Produccion.Enfermedades_y_control_de_estanques;
using BussinessLogicTecnoPez.Produccion.Clasificacion_e_Incubacion;

public partial class Modules_EnfermedadesControlEstanque_ControlesPorEstanques : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        cargarDatos();
        Globals_GenericMasterPageNoSecurity master = (Globals_GenericMasterPageNoSecurity)this.Master;
        master.Imagen = "../Styles/Icons/controls.png";
        master.Titulo = "Gestión de Controles a Estanques";
    }

    private int IdEstanque
    {
        get { return (int)this.ViewState["idestanque"];}
        set {this.ViewState["idestanque"] = value;}
    }

    private Especie PEspecie
    {
        get { return this.ViewState["especie"] as Especie; }
        set { this.ViewState["especie"] = value; }
    }


    private void cargarDatos()
    {
        this.IdEstanque = Convert.ToInt32(Request["idEstanque"]);

        Clasificacion objClasificacion = new Clasificacion();
        objClasificacion.obtenerUltima(this.IdEstanque);
        this.PEspecie = objClasificacion.especie;

        List<ParametroControl> listaParametros = ParametroControl.getManager.GetAll();

        repet.DataSource = listaParametros;
        repet.DataBind();
    }
    protected void repet_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ((Label)e.Item.FindControl("lblParameterName")).Text = (String)DataBinder.GetPropertyValue(e.Item.DataItem, "Descripcion");
        ((GridView)e.Item.FindControl("grdData")).DataSource = Controles.obtenerControles(this.IdEstanque, (int)DataBinder.GetPropertyValue(e.Item.DataItem, "IdParametroControl"));
        ((GridView)e.Item.FindControl("grdData")).DataBind();

        ((GridView)e.Item.FindControl("grdData")).RowStyle.CssClass = "GrdRow";
        ((GridView)e.Item.FindControl("grdData")).HeaderStyle.CssClass = "GrdHeader";
        ((GridView)e.Item.FindControl("grdData")).AlternatingRowStyle.CssClass = "GrdAlternatingRow";
        ((GridView)e.Item.FindControl("grdData")).PagerStyle.CssClass = "GrdPager";

        ((GridView)e.Item.FindControl("grdData")).ControlStyle.CssClass = "GrdHeader";
        ((GridView)e.Item.FindControl("grdData")).HeaderStyle.ForeColor = System.Drawing.Color.White;
        ((GridView)e.Item.FindControl("grdData")).PagerStyle.ForeColor = System.Drawing.Color.White;

        string url = "InsertarControl.aspx?idestanque=" + this.IdEstanque.ToString() + "&idparametro=" + (DataBinder.GetPropertyValue(e.Item.DataItem, "IdParametroControl")).ToString();
        string strparam = " width= 550,height= 400, scrollbars=NO, resizable=yes, top=200,left=300,status=yes ";

        ((Button)e.Item.FindControl("btnIngresar")).OnClientClick = "javascript:window.open('" + url + "','InsertarControl', '" + strparam + "'); return false;";
    }

    protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.Cells.Count == 0) return;
        if (e.Row.Cells[4].Controls.Count == 0) return;

        Image img = (Image)e.Row.Cells[4].Controls[0];
        img.Style.Add("cursor", "pointer");

        decimal valorObtenido;
        valorObtenido = (decimal)DataBinder.GetPropertyValue(e.Row.DataItem, "valorObtenido");
        int idParametro = (int)DataBinder.GetPropertyValue(e.Row.DataItem, "idParametroControl");

        img.ImageUrl = "../../Styles/Icons/ledblue.png";
        img.ToolTip = "No hay valores cargados de parámetros de control para esta especie";
        if (DataBinder.GetPropertyValue(e.Row.DataItem, "valorMaximo") is System.DBNull==false)
        {
            if (DataBinder.GetPropertyValue(e.Row.DataItem, "valorMinimo") is System.DBNull == false)
            {
               decimal valorMaximo = (decimal) DataBinder.GetPropertyValue(e.Row.DataItem, "valorMaximo");
               decimal valorMinimo = (decimal)DataBinder.GetPropertyValue(e.Row.DataItem, "valorMinimo");

               if (valorObtenido > valorMaximo || valorObtenido < valorMinimo)
                {
                    img.ImageUrl = "../../Styles/Icons/ledred.png";
                    img.ToolTip = "EL valor " + valorObtenido.ToString() + " está fuera del rango recomendado";
                }
                else
                {
                    img.ImageUrl = "../../Styles/Icons/ledgreen.png";
                    img.ToolTip = "EL valor " + valorObtenido.ToString() + " está dentro del rango recomendado";
                }
            }
        }
    }
}
